package com.celesea.auth.group.v1.controller;

import com.celesea.auth.group.service.GroupService;
import com.celesea.auth.group.v1.controller.request.PostRequest;
import com.celesea.auth.group.v1.controller.request.PutRequest;
import com.celesea.auth.group.v1.controller.resource.FormResource;
import com.celesea.auth.group.vo.GroupVo;
import com.celesea.auth.role.entity.RoleCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author JackYuan
 * @create 2017-10-26 23:30
 */
@Api(tags = "群组接口")
@RestController
@RequestMapping("v1/groups")
@PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
public class GroupController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private GroupService groupService;

    @PostMapping
    @ApiOperation(value = "保存群组")
    public FormResource post(@RequestBody @Valid PostRequest request) {
        GroupVo role = groupService.insert(new GroupVo(request));
        return new FormResource(role);
    }

    @PutMapping
    @ApiOperation(value = "更新群组")
    public FormResource put(@RequestBody @Valid PutRequest request) {
        GroupVo role = groupService.update(new GroupVo(request));
        return new FormResource(role);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询群组")
    public FormResource get(@PathVariable String id) {
        GroupVo role = groupService.selectById(id);
        return new FormResource(role);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据id删除群组")
    public void delete(@PathVariable String id) {
        groupService.doDelete(id);
    }

    @PutMapping(value = "/{id}/disable")
    public void disable(@PathVariable String id) {
        groupService.disable(id);
    }

    @PutMapping(value = "/{id}/enable")
    public void enable(@PathVariable String id) {
        groupService.enable(id);
    }
}
